#!/bin/bash

require() {
  local plugin_name=$1
  local plugin_version=$2
  local plugin_repo=$3
  local force_update=$IS_REQUIRE_MOD_UPDATE_TMP_GLOBAL

  if [ "$(zmicro plugin has $plugin_name)" = "true" ]; then
    log::debug "[$(timestamp)][require] installed before (2): ${plugin_name}"

    if [ "$force_update" = "true" ]; then
      log::info "[$(timestamp)][require] update: ${plugin_name}"
      zmicro plugin update $plugin_name
    fi
    return
  fi

  log::info "[$(timestamp)][require] install: $plugin_name ($plugin_version) ..."

  # log::info "[$(timestamp)][require] repo: ${plugin_repo}"

  # @TODO
  zmicro plugin install $plugin_name $plugin_version $plugin_repo
  if [ "$?" = "0" ]; then
    log::success "[$(timestamp)][require] install success: ${plugin_name}"
  else
    log::error "[$(timestamp)][require] install failed: ${plugin_name}"
  fi

  # sleep 1
}

require::parse() {
  # echo "xxx: $@"
  # compose@v0.0.1
  local text=$2

  # echo "IS_REQUIRE_MOD_UPDATE_TMP_GLOBAL: $IS_REQUIRE_MOD_UPDATE_TMP_GLOBAL"
  # echo "is_require_mod_update: $is_require_mod_update"

  local plugin_name=$(echo $text | awk -F '@' '{print $1}')
  local plugin_version=$(echo $text | awk -F '@' '{print $2}' | awk -F '#' '{print $1}')
  local plugin_repo=$(echo $text | awk -F '#' '{print $2}')

  # echo "[require::parse] ${plugin_name}(${plugin_version}) - ${plugin_repo}"
  # log::info "[$(timestamp)][require] install plugin $plugin_name ($plugin_version) - ${plugin_repo} ..."
  require $plugin_name $plugin_version $plugin_repo
}

require::mod() {
  # @TODO overide core DEPENDENCIES
  if [ -n ${DEPENDENCIES} ]; then
    # echo "overide DEPENDENCIES"
    DEPENDENCIES=()
  fi

  local mod_path=$1
  local is_require_mod_update=false

  shift
  while [[ $# -gt 0 ]]; do
    local key=$1
    local value=$2
    case $key in
    -u | --update)
      os::set_var is_require_mod_update true
      shift
      ;;
    *)
      if [ "$(string::match $key ^-)" = "true" ]; then
        log::error "[require::mod] ERROR: unknown parameter \"$key\""
        exit 1
      fi

      shift
      ;;
    esac
  done

  # @TODO cannot pass params in array::each, so use global instead of
  IS_REQUIRE_MOD_UPDATE_TMP_GLOBAL=$is_require_mod_update

  # load mod
  config::load_file $mod_path

  if [ -n $DEPENDENCIES ]; then
    local dependeny_count=$(array::length ${DEPENDENCIES[@]})
    if [ "$dependeny_count" = "0" ]; then
      return
    fi

    log::info "[$(timestamp)][require] found depdencies: $(color::green ${dependeny_count}) ..."
    echo ""

    array::each require::parse ${DEPENDENCIES[@]}

    # echo $DEPENDENCIES
    # for one in ${DEPENDENCIES[@]}; do
    #   echo "x: "$one
    # done

    # @TODO clean
    # export DEPENDENCIES=""
  fi
}

require::core_mod() {
  require::mod $ZMICRO_MOD $@
}

require::plugin_mod() {
  local plugin_name=$1
  if [ -z $plugin_name ]; then
    log::error "[require::plugin_mod] plugin name is required"
    exit 1
  fi

  # echo "plugin_name: $(plugin::get_mod_path $plugin_name)"
  # echo "${DEPENDENCIES[@]}"
  require::mod $(plugin::get_mod_path $plugin_name) $@
}

export -f require
export -f require::mod
export -f require::parse

export -f require::core_mod
export -f require::plugin_mod
